package cn.cw.myrxjavademo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tamic.novate.BaseSubscriber;
import com.tamic.novate.Novate;
import com.tamic.novate.Throwable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * @author zhou_hp_pc
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Logger.addLogAdapter(new AndroidLogAdapter());

//        getWeather();
        getSemanticForI("你好");
    }

    private Novate novate;

    private void getWeather() {

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("province", "北京");
        parameters.put("city", "北京");
        parameters.put("key", "1ae92e31ca0cc");

        novate = new Novate.Builder(this)
                .addParameters(parameters)
                .connectTimeout(10)
                .baseUrl("http://apicloud.mob.com/")
                .addLog(true)
                .build();

        novate.get("v1/weather/query", parameters, new BaseSubscriber<ResponseBody>() {
            @Override
            public void onError(Throwable e) {
                Logger.e(e.getMessage());
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                String jstr = null;
                try {
                    jstr = new String(responseBody.bytes());
                    Logger.e(jstr);
                } catch (IOException e) {
                    e.printStackTrace();
                }

//                Type type = new TypeToken<MovieModel>() {}.getType();
//
//                MovieModel response = new Gson().fromJson(jstr, type);


            }
        });
    }

    private  String URL = "http://icsp.bjrcb.com/robot-bjnsbank/ws/RobotServiceEx4Mms";
    private  String baseUrl = "http://icsp.bjrcb.com/";

    private void getSemanticForI(String result) {
        String soapRequestData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:rob=\"http://www.eastrobot.cn/ws/RobotServiceEx\">"
                + " <soapenv:Header/>" + "<soapenv:Body>" + "  <rob:deliver>" + "     <robotRequest>"
                + "        <attributes>" + "           <name>platform</name>" + "           <value>solid</value>"
                + "        </attributes>" + "        <attributes>" + "<name>brand</name>"
                + "           <value>business</value>" + "        </attributes>"
                + "        <maxReturn>10</maxReturn>" + "        <question>" + result+ "</question>"
                + "        <sessionId>" + "user0001" + "</sessionId>" + "        <userId>" + "user0001"
                + "</userId>" + "     </robotRequest>" + "  </rob:deliver>" + "  </soapenv:Body>"
                + "</soapenv:Envelope>";
        novate = new Novate.Builder(this)
                .connectTimeout(8)
                .baseUrl(baseUrl)
                .addLog(true)
                .build();


        /**
         *
         *
         * 调用post需要你自己解析数据
         *
         * 如果需要解析后返回 则调用novate.executeGet()
         * 参考 performGet()中的方式
         */
        novate.json("robot-bjnsbank/ws/RobotServiceEx4Mms", soapRequestData, new BaseSubscriber<ResponseBody>() {
            @Override
            public void onError(Throwable e) {
                Logger.e(e.getMessage());
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                String jstr = null;
                try {
                    jstr = new String(responseBody.bytes());
                    Logger.xml(jstr);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
